<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class FormView {
	
	var $_html		= 	'';
	var $_event		=	'';
	
	var $form_bean	=	'';
	var $form_data	=	'';
	
	var $form_id	=	'';
	var $form_action	=	'';
	var $form_rules		=	'';
	
	var $form_complete	=	array();
	//form提交方式：submit、ajax
	var $form_submit_type	= 'submit';
	
	
	var $table_class	=	'formTable';
	var $submit_class	=	'';
	
	/**
	 * Constructor
	 *
	 * @access	public
	 * @param	array	initialization parameters
	 */
	public function __construct($params = array())
	{
		if (count($params) > 0)
		{
			$this->initialize($params);
		}

		log_message('debug', "FormView Class Initialized");
	}

	// --------------------------------------------------------------------

	/**
	 * Initialize Preferences
	 *
	 * @access	public
	 * @param	array	initialization parameters
	 * @return	void
	 */
	function initialize($params = array())
	{
		if (count($params) > 0)
		{
			foreach ($params as $key => $val)
			{
				if (isset($this->$key))
				{
					$this->$key = $val;
				}
			}
		}
	}

	// --------------------------------------------------------------------
	
	function getFormItem($item,$item_init_data,$item_item,$item_id,$item_name,$item_value) {
		$res = '';
		$item_items = explode(':',$item_item,2);
		$item_item = $item_items[0];
		$item_item_attr = count($item_items)==1?'':$item_items[1];
		
		switch ($item_item) {
			case 'password':
			case 'hidden':
			case 'text':
				$res = '<input '.$item_item_attr.' type="'.$item_item.'" id="'.$item_id.'" name="'.$item_name.'" value="'.$item_value.'" />';
		       	break;
			case 'label':
				$res = '<label '.$item_item_attr.' id="'.$item_id.'">'.$item_value.'</label> ';
				break;
			case 'editor':
			case 'textarea':
				$res = '<textarea '.$item_item_attr.' id="'.$item_id.'" name="'.$item_name.'" cols="60">'.$item_value.'</textarea>';
				break;
			case 'radio':
				$item_item_attr = json_decode($item_item_attr);
				$checked = '';
				foreach ($item_item_attr as $key=>$value) {
					$checked = $key === $item_value?'checked':'';
					$res .= '<input type=radio name='.$item_name.' value='.$key.' '.$checked.'/>'.$value;
				}
				break;
			case 'checkbox':
				$item_item_attr = $item_init_data?$item_init_data:json_decode($item_item_attr);
				foreach ($item_item_attr as $key=>$value) {
					$checked = $key === $item_value?'checked':'';
					$res .= '<label><input type="checkbox" id="'.$item_id.'_'.$key.'" name="'.$item_name.'[]" value='.$key.' '.$checked.'/>'.$value.'</label>';
				}
				break;
			case 'upfile':
				
				break;
			case 'select':
				$item_item_attr = $item_init_data?$item_init_data:json_decode($item_item_attr);
				$selected = '';
				$res .= '<select id="'.$item_id.'" name="'.$item_name.'">';
				foreach ($item_item_attr as $key=>$value) {
					$selected = $key === $item_value?'selected':'';
					$res .= '<option value='.$key.' '.$selected.'>'.$value.'</option>';
				}
				$res .= '</select>';
				break;
			case 'captcha':
				$res = '<input '.$item_item_attr.' type="text" id="'.$item_id.'" name="'.$item_name.'" value="'.$item_value.'" />
				<img width="45" height="22" src="'.site_url('captcha/mixed?').time().'" id="img_'.$item_id.'">
				<a href="javascript:void(0);" id="re_'.$item_id.'">看不清</a>';
				$this->_event = '
					document.getElementById("re_'.$item_id.'").onclick = function(){
						var captcha = document.getElementById("img_'.$item_id.'");
						captcha.src = captcha.src.substring(0,captcha.src.indexOf("?")+1)+new Date().getTime();
					};';
				break;
			case 'submit':
				if(isset($item['type']))$this->form_submit_type = $item['type'];
			case 'button':
				if(isset($item['type']))$this->form_submit_type = $item['type'];
			case 'reset':
				if(count($this->form_complete)==0 && isset($item['complete']) && is_array($item['complete'])){
					$this->form_complete = $item['complete'];
				}
				$res = '<input '.$item_item_attr.' class="'.$this->submit_class.'" type="'.$item_item.'" '.($item_id?"id=$item_id":'').' value="'.$item_value.'" />';
		       	break;
		}
		return $res;
	}
	/*
	function getJSEvent($filed,$this->_events) {
		$binds = "";
		foreach ($this->_events as $this->_event => $fn) {
			//$binds .= bind(document.getElementById("$filed"),"$this->_event",$fn);
			
			$binds .= "$('#".$filed."').".$this->_event."($fn)";
		}
		return $binds;
	}*/
	
	function setForm($form_bean) {
		
		foreach ($form_bean as $item) {
			
			$item_label	= isset($item['label']) ? $item['label'] : NULL;
			$item_field	= isset($item['field']) ? $item['field'] : NULL;
			$item_item	= isset($item['item']) ? $item['item'] : NULL;//item类型
			$item_value = isset($item['value']) ? $item['value'] : NULL;
			$item_rules = isset($item['rules']) ? $item['rules'] : NULL;
			$item_msg	= isset($item['msg']) ? $item['msg'] : NULL;
			$item_event	= isset($item['event']) ? $item['event'] : NULL;//数组
			
			$item_id	= $item_field;
			$item_name	= $item_field;
			$item_bean_name = NULL;
			
			$item_init_data	= NULL;//select,radio 动态初始化数据 $this->form_data['init'][$item_bean_name][$item_field]
			
			if ($item_field) {
				if(strpos($item_id,'[')){
					$item_bean_name_s = explode('[',$item_id,2);
					$item_bean_name	= $item_bean_name_s[0];
					$item_id = str_replace(']','_',str_replace('[','_',$item_bean_name_s[1]));
					$item_id = $item_bean_name.'_'.substr($item_id,0,strlen($item_id)-1);
					$item_field = substr($item_bean_name_s[1],0,strlen($item_bean_name_s[1])-1);
				}
				
				if ($this->form_data) {
					if(isset($this->form_data[$item_bean_name][$item_field])){
						$item_value = $this->form_data[$item_bean_name][$item_field];
					}else if(isset($this->form_data[$item_field])){
						$item_value = $this->form_data[$item_field];
					}
				}
				
				if(isset($this->form_data['init'])){
					if(isset($this->form_data['init'][$item_field])){
						$item_init_data = $this->form_data['init'][$item_field];
					}else if(isset($this->form_data['init'][$item_bean_name][$item_field])){
						$item_init_data = $this->form_data['init'][$item_bean_name][$item_field];
					}
				}

				if ($item_rules) {
					if ( strpos($item_rules,'is_exist:') > -1 && $item_value)
						$item_rules = str_replace('is_exist:','is_exist:'.$item_value.'@',$item_rules);
					$this->form_rules .= $item_id.':\''.$item_rules.'\',';
				}
			}
		
			if ($item_event) {
				$this->_event .= $item_event;
			}
			
			if (isset($item_label)) {
				if ($item_id && $item_label) {
					$this->_html .= '<tr><th width="120"><label for="'.$item_id.'"><tt id="'.$item_id.'_name">'.$item_label.'</tt></label>：</th><td>';
				} else if ($item_label == ''){
					$this->_html .= '<tr><td width="120">&nbsp;</td><td>';
				} else{
					$this->_html .= '<tr><th width="120"><label>'.$item_label.'</label>：</th><td>';
				}
			}
			
			if ($item_item) {
				$this->_html .= $this->getFormItem($item,$item_init_data,$item_item,$item_id,$item_name,$item_value);
			}else if (isset($item['items'])){
				$this->setForm($item['items']);
			}
			
			if ($item_id && $item_msg) {
				$this->_html .= '<div id="'.$item_id.'_tip">'.$item_msg.'</div>';
			}
			
			if (isset($item_label)) {
				$this->_html .= '</td></tr>';
			}
		}
	}
	
	function getFormComplete(){

		if(count($this->form_complete) == 0){
			$this->form_complete = array(
				'reset'		=>	false,
				'callback'	=>	null,
				'redirect'	=>	'',
				'template'	=>	''
			);
		}else{
			if(isset($this->form_complete['template']) && $this->form_complete['template']){
				$this->form_complete['template'] = APPPATH.'views'.$this->form_complete['template'];
			}
		}
		return json_encode($this->form_complete);
	}
	
	function getForm($form_id='',$form_action='',$form_bean='',$form_data='',$submit_type=''){
		if($form_id && $form_action && $form_bean){
			$this->form_id	=	$form_id;
			$this->form_action	=	$form_action;
			$this->form_bean	=	$form_bean;
			$this->form_data	=	$form_data;
			
			$this->_html	= 	'<form id="'.$this->form_id.'" name="'.$this->form_id.'" method="post">
				<table class="'.$this->table_class.'" border="0" cellpadding="0" cellspacing="0">';
			$this->setForm($this->form_bean);
			$this->_html	.=	'</table></form>';
			
			if($submit_type)
				$this->form_submit_type = $submit_type;
			
			$this->form_rules = '{'.substr($this->form_rules,0,strlen($this->form_rules)-1).'}';
			$script = '<script>var '.$this->form_id.' = new plugor.form({id:"'.$this->form_id.'",action:"'.$this->form_action.'",type:"'.$this->form_submit_type.'",rules:'.$this->form_rules.',complete:'.$this->getFormComplete().'});'.$this->_event.'</script>';
		
			return $this->_html.$script;
		}else{
			return 'FormView 缺少参数！';
		}
	}
}
?>